#include "CPlayerTank.h"
#include "SGD Wrappers\CSGD_TextureManager.h"
#include "Game.h"

CPlayerTank::CPlayerTank()
{
	m_fCurrentRotation	= 0.0f;
	m_fMaxForwardSpeed	= 130.0f;
	m_fMaxReverseSpeed	= 100.0f;
	m_fCurrSpeed		= 0.0f;
	m_fAccel			= 100.0f;
	m_fRotationRate		= 3.14f*0.25f;
	m_nWidth			= 128;
	m_nHeight			= 128;

	m_tVelocity.fX = 0;
	m_tVelocity.fX = 0;

	m_nType = OBJ_TANK;

	m_tFacingDir.fX = 0.0f;
	m_tFacingDir.fY = -1.0f;
}

void CPlayerTank::Update(float _fElapsedTime)
{
	if( ! CSGD_FModManager::GetInstance()->IsSoundPlaying(m_nSoundIDInterior))
		CSGD_FModManager::GetInstance()->PlaySoundA(m_nSoundIDInterior);
	
	if(m_fPosX+m_nWidth/2 > CGame::GetInstance()->GetWindowWidth())
	{
		m_fPosX = float(CGame::GetInstance()->GetWindowWidth() - m_nWidth/2);
		m_fVelX = 0.0f;
	}else if(m_fPosX-m_nWidth/2 < 0)
	{
		m_fPosX = m_nWidth/2.0f;
		m_fVelX = 0.0f;
	}

	if(m_fPosY+m_nHeight/2 > CGame::GetInstance()->GetWindowHeight())
	{
		m_fPosY = float(CGame::GetInstance()->GetWindowHeight() - m_nHeight/2);
		m_fVelY = 0.0f;
	}else if(m_fPosY-m_nHeight/2 < 0)
	{
		m_fPosY = m_nHeight/2.0f;
		m_fVelY = 0.0f;
	}

	if (CSGD_DirectInput::GetInstance()->KeyDown(DIK_RIGHT))
	{
		if(m_fCurrSpeed<0)
			m_fCurrentRotation -= m_fRotationRate * _fElapsedTime;
		else
			m_fCurrentRotation += m_fRotationRate * _fElapsedTime;
	}

	if (CSGD_DirectInput::GetInstance()->KeyDown(DIK_LEFT))
	{
		if(m_fCurrSpeed<0)
			m_fCurrentRotation += m_fRotationRate * _fElapsedTime;
		else
			m_fCurrentRotation -= m_fRotationRate * _fElapsedTime;
	}
	if (CSGD_DirectInput::GetInstance()->KeyDown(DIK_UP))
	{
		//velocity increases
		m_tFacingDir = Vector2DRotate(m_tFacingDir, m_fCurrentRotation);

		if(m_fCurrSpeed<0)
			m_fCurrSpeed += m_fAccel * 2.0f * _fElapsedTime;
		else
			m_fCurrSpeed += m_fAccel * _fElapsedTime;

		if(m_fCurrSpeed > m_fMaxForwardSpeed)
			m_fCurrSpeed = m_fMaxForwardSpeed;

		m_tVelocity.fX = m_tFacingDir.fX * m_fCurrSpeed * _fElapsedTime;
		m_tVelocity.fY = m_tFacingDir.fY * m_fCurrSpeed * _fElapsedTime;

		m_tFacingDir.fX = 0.0f;
		m_tFacingDir.fY = -1.0f;
	}

	if (CSGD_DirectInput::GetInstance()->KeyDown(DIK_DOWN))
	{
		//velocity decreases
		m_tFacingDir = Vector2DRotate(m_tFacingDir, m_fCurrentRotation);

		if(m_fCurrSpeed>0)
			m_fCurrSpeed -= m_fAccel * 2.0f * _fElapsedTime;
		else
			m_fCurrSpeed -= m_fAccel * _fElapsedTime;

		if(m_fCurrSpeed < -m_fMaxReverseSpeed)
			m_fCurrSpeed = -m_fMaxReverseSpeed;

		m_tVelocity.fX = m_tFacingDir.fX * m_fCurrSpeed * _fElapsedTime;
		m_tVelocity.fY = m_tFacingDir.fY * m_fCurrSpeed * _fElapsedTime;

		m_tFacingDir.fX = 0.0f;
		m_tFacingDir.fY = -1.0f;
	}

	if (CSGD_DirectInput::GetInstance()->KeyUp(DIK_UP) && CSGD_DirectInput::GetInstance()->KeyUp(DIK_DOWN))
	{
		if(m_fCurrSpeed>m_fMaxForwardSpeed*0.1f)
		{
			m_tFacingDir = Vector2DRotate(m_tFacingDir, m_fCurrentRotation);

			m_fCurrSpeed -= m_fAccel * _fElapsedTime;
			m_tVelocity.fX = m_tFacingDir.fX * m_fCurrSpeed * _fElapsedTime;
			m_tVelocity.fY = m_tFacingDir.fY * m_fCurrSpeed * _fElapsedTime;
			
			m_tFacingDir.fX = 0.0f;
			m_tFacingDir.fY = -1.0f;
		}
		else if(m_fCurrSpeed<-m_fMaxReverseSpeed*0.1f)
		{
			m_tFacingDir = Vector2DRotate(m_tFacingDir, m_fCurrentRotation);

			m_fCurrSpeed += m_fAccel * _fElapsedTime;
			m_tVelocity.fX = m_tFacingDir.fX * m_fCurrSpeed * _fElapsedTime;
			m_tVelocity.fY = m_tFacingDir.fY * m_fCurrSpeed * _fElapsedTime;
			
			m_tFacingDir.fX = 0.0f;
			m_tFacingDir.fY = -1.0f;
		}
		else
		{
			m_fCurrSpeed = 0;
			m_tVelocity.fX = 0.0f;
			m_tVelocity.fY = 0.0f;
		}
	}

	if (CSGD_DirectInput::GetInstance()->KeyPressed(DIK_SPACE))
	{
		CSGD_FModManager::GetInstance()->PlaySoundA(m_nSoundIDShoot);
	}

	// PLAY MOVEMENT SOUNDS
	if(m_fCurrSpeed==0)
	{
		CSGD_FModManager::GetInstance()->StopSound(m_nSoundIDMove);
	}
	else
	{
		if(!CSGD_FModManager::GetInstance()->IsSoundPlaying(m_nSoundIDMove))
			CSGD_FModManager::GetInstance()->PlaySoundA(m_nSoundIDMove);
	}

	m_fPosX += (m_tVelocity.fX);
	m_fPosY += (m_tVelocity.fY);

}

void CPlayerTank::Render()
{
	float fOffsetX = -m_nWidth * 0.5f;
	float fOffsetY = -m_nHeight * 0.5f;
	
	RECT shipRect = {0,0,m_nWidth,m_nHeight};
	CSGD_TextureManager::GetInstance()->Draw(
		GetTexID(),
		(int)(m_fPosX + fOffsetX),
		(int)(m_fPosY + fOffsetY), 
		1.0f, 1.0f, 
		&shipRect, 
		m_nWidth*0.5f, 
		m_nHeight*0.5f, 
		m_fCurrentRotation);
}

RECT CPlayerTank::GetCollisonRect()
{
	float fOffsetX = -m_nWidth * 0.5f;
	float fOffsetY = -m_nHeight * 0.5f;
	
	RECT collisonRect = {(long)(m_fPosX + fOffsetX),(long)(m_fPosY + fOffsetY),(long)m_fPosX + m_nWidth,(long)m_fPosY+ m_nHeight};

	return collisonRect;
}